<template>
  <div class="login">
    <div class="picture">
<!--      <img src="@/assets/loginBack.jpeg" alt="">-->
    </div>
    <div class="form-content">
      <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="120px"
          class="demo-ruleForm"
      >

        <el-form-item label="账户" prop="name" class="labels">
          <el-input
              v-model="ruleForm.name"
              autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"
          >Submit</el-button
          >
          <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>

</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
import Service from "../../data/Services.js";
import { ElMessage } from 'element-plus'

const ruleFormRef = ref<FormInstance>()

const checkAge = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error('Please input the age'))
  }
  setTimeout(() => {
    if (!Number.isInteger(value)) {
      callback(new Error('Please input digits'))
    } else {
      if (value < 18) {
        callback(new Error('Age must be greater than 18'))
      } else {
        callback()
      }
    }
  }, 1000)
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password'))
  } else {
    if (ruleForm.name !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('name', () => null)
    }
    callback()
  }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password again'))
  } else if (value !== ruleForm.pass) {
    callback(new Error("Two inputs don't match!"))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  pass: '',
  name: '',
})

const rules = reactive({
  pass: [{ validator: validatePass, trigger: 'blur' }],
  name: [{ validator: validatePass2, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('submit!')
      checkIsLogin("1","1")
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const checkIsLogin = (user:any,password:any)=>{
  const open1 = () => {
    ElMessage({
      message: '登录成功',
      type: 'success',
    })
  }
  Service().isLogin(user,password).then((data: any)=>{
    if(data.code == 200 && data.data == "success"){
      open1();
      window.location.href = window.location.origin  +"/#/Home";
      setCookie("user", user,1);
    }
  }).catch((err:any)=>{
    console.log(err)
  })
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

//写入cookie
const setCookie=(name, value, exdays) => {
    //name  :表示cookie的名称，比如token
    //value  :表示cookie的值
    //exdays  :表示cookie的有效时间
    let d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    let expires = "expires=" + d.toUTCString();
    document.cookie = name + "=" + value + "; " + expires;
  }

</script>

<style scoped lang="less">
.login{
  display: flex;
  height: 100%;
  min-height: 809px;
  background: #a79e9e;
  .picture{
    width: 50%;
    height: 100%;
    img{
      width: 100%;
      display: block;
    }
  }
  .form-content{
    width: 50%;
    background: #ffffffcf;
    display: flex;
    align-items: center;
    justify-content: center;
    .demo-ruleForm{
      width: 460px;
      height: 170px;
      .el-form-item__label{
        color: white;
      }
      .labels{
        //height: 100%;
        .el-form-item__label{
          color: white;
        }
      }
      }
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .text {
        font-size: 14px;
      }

      .item {
        margin-bottom: 18px;
      }

      .box-card {
        width: 480px;
      }
    }

  }
}

</style>
